﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Payroll.Common.Utils;
using Payroll.BusinessLogic;
using Payroll.BusinessLogic.Directory;
using Payroll.BusinessLogic.CustomerManager;
using PayrollHR.Web;
using System.Data;
using MigraDoc.DocumentObjectModel;
using Payroll.BusinessLogic.Utils;

namespace PayrollHR.Administration.Modules
{
    public partial class CaseProfileProperty : BasePayrollAdministrationUserControl
    {
        public int CustomerID
        {
            get
            {
                return CommonHelper.QueryStringInt("CustomerId");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.FillObjectIDDropDowns();
            }
        }
        private void FillObjectIDDropDowns()
        {
            ddlObjectID.Items.Clear();
            var FeesTypeCollection = this.CustomerService.GetObjectById(7);
            if (FeesTypeCollection.Count != 1)
            {
                FeesTypeCollection.Insert(0, new Objects { ObjectId = 0, ObjectName = "-None-" });
            }
            foreach (Objects feestype in FeesTypeCollection)
            {
                ListItem ddlFeesTypeItem2 = new ListItem(feestype.ObjectName, feestype.ObjectId.ToString());
                ddlObjectID.Items.Add(ddlFeesTypeItem2);
            }
        }
        public void DataBind(Customer customer)
        {
            if (CommonHelper.IsNull(customer))
                return;
            txtProPropertyAccountValue.Text = customer.ProPropertyAccountValue;
            txtProPropertyCar.Text = customer.ProPropertyCar;
            txtProPropertyLand.Text = customer.ProPropertyLand;
            txtProPropertyOther.Text = customer.ProPropertyOther;
            List<profile> profile = CustomerService.GetAllProfileByCustomerID(CustomerID, 7);
            BindGrid(profile);
        }
        protected void BindGrid(List<profile> dt)
        {
            ViewState["CaseProfileProperty"] = dt;
            gvObject.DataSource = dt;
            gvObject.DataBind();
        }
        protected void gvObject_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvObject.PageIndex = e.NewPageIndex;
            var customer = CustomerService.GetAllProfileByCustomerID(CustomerID, 7);
            BindGrid(customer);
        }
        private List<profile> getTableGridview()
        {
            List<profile> dtCurrentTable = new List<profile>();
            if (ViewState["CaseProfileProperty"] != null)
            {
                dtCurrentTable = (List<profile>)ViewState["CaseProfileProperty"];
            }
            return dtCurrentTable;
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            List<profile> listProfile = getTableGridview();
            profile profile = new profile()
            {
                ProfileID = -1,
                ObjectID = CommonHelper.ToInteger(ddlObjectID.SelectedItem.Value),
                ObjectName = ddlObjectID.SelectedItem.Text,
                OriginalCopy = cbOriginalCopy.Checked,
                duplicate = cbNotOriginalCopy.Checked,
                OnRequest = dpOnRequest.SelectText,
                DateSubmitted = dp1DateSubmitted.SelectText,
                Note = txtNote.Text,
                UpdateByID = 1,
                CreateByID = 1,
                CreatedOn = Convert.ToDateTime(DateTime.Today),
                UpdatedOn = Convert.ToDateTime(DateTime.Today)
            };
            listProfile.Add(profile);
            BindGrid(listProfile);
        }
        public Customer GetCustomer(Customer customer)
        {
            customer.ProPropertyAccountValue = txtProPropertyAccountValue.Text;
            customer.ProPropertyCar = txtProPropertyCar.Text;
            customer.ProPropertyLand = txtProPropertyLand.Text;
            customer.ProPropertyOther = txtProPropertyOther.Text;
            return customer;
        }
        public List<profile> GetProfile(ref List<profile> List)
        {
            if (ViewState["CaseProfileProperty"] != null)
            {
                List<profile> dt = (List<profile>)ViewState["CaseProfileProperty"];
                for (int i = 0; i < dt.Count; i++)
                {
                    List.Add(dt[i]);
                }
            }
            return List;
        }

       
        protected void gvObject_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int index = e.RowIndex;
                GridViewRow row = gvObject.Rows[index];
                HiddenField hfProfileID = row.FindControl("hfProfileID") as HiddenField;
                List<profile> listProfile = getTableGridview();
                if (listProfile != null)
                {
                    listProfile.RemoveAt(index);
                    if (CommonHelper.ToInteger(hfProfileID.Value) > 0)
                    {
                        List<int> List = new List<int>();
                        List.Add(CommonHelper.ToInteger(hfProfileID.Value));
                        this.CustomerService.DeleteProfile(List);
                    }
                    BindGrid(listProfile);
                }
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }

        protected void gvObject_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton btnDelete = e.Row.FindControl("lbDelete") as LinkButton;
                if (btnDelete != null)
                    btnDelete.CommandArgument = e.Row.RowIndex.ToString();
            }
        }

       
    }
}